import { _decorator, Component, Node, UITransform, Sprite, Animation, SpriteFrame, AnimationClip, animation } from 'cc'
const { ccclass, property } = _decorator


import {  TILE_WIDTH, TILE_HEIGHT } from '../Scripts/Tile/TileMapManager'


// 状态机
import {StateMchine} from './StateMchine'

// 状态机类型
import { DIRECTION, PLAYER_STATE_TYPE_ENUM, ENTITY_TYPE_ENUM } from '../Enums'

// 实体类型
import { IEntity } from '../Levels'

// utils
import { generateRandomId } from '../Utils'


@ccclass('EntityManager')
export class EntityManager extends Component {
  id = generateRandomId()
  type: ENTITY_TYPE_ENUM = ENTITY_TYPE_ENUM.PLAYER

  x: number = 0
  y: number = 0

  // 状态机
  fsm: StateMchine | null = null
  // 设置方向和动画
  private _direction: DIRECTION = DIRECTION.TOP
  private _state: PLAYER_STATE_TYPE_ENUM = PLAYER_STATE_TYPE_ENUM.IDLE

  get direction() {
    return this._direction
  }

  set direction(value: DIRECTION) {
    this._direction = value
    // 通过状态去设置参数
    this.fsm?.setPramers(PLAYER_STATE_TYPE_ENUM.DIRECTION, value)
  }

  get state() {
    return this._state
  }

  set state(value: PLAYER_STATE_TYPE_ENUM) {
    this._state = value
    // 通过状态去设置参数
    this.fsm?.setPramers(value, true)
  }

  init(params: IEntity) {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM

    // 设置大小
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4)

    this.x = params.x
    this.y = params.y
    this.direction = params.direction
    this.state = params.state
    this.type = params.type

    // // 初始化状态机
    // this.fsm = this.addComponent(PlayerStateMchine)
    // await this.fsm.init()
    // this.direction = DIRECTION.TOP
    // this.state = PLAYER_STATE_TYPE_ENUM.IDLE

    // // 注册事件
    // EventManager.Instance.on('DIRECTION', this.moveTo, this)
  }

  // 每帧更新
  update() {
    this.node.setPosition(this.x * TILE_WIDTH - 1.5 * TILE_WIDTH, -this.y * TILE_HEIGHT + 1.5 * TILE_HEIGHT)
  }

  onDestroy() {

  }

  // updateXY() {
  //   if (this.targetX < this.x) {
  //     this.x -= STEP
  //   } else if (this.targetX > this.x) {
  //     this.x += STEP
  //   }

  //   if (this.targetY < this.y) {
  //     this.y -= STEP
  //   } else if (this.targetY > this.y) {
  //     this.y += STEP
  //   }
  //   // 判断最后是否到达目标点，无限接近
  //   const infinitesimal = 0.1
  //   if (Math.abs(this.targetX - this.x) <= infinitesimal && Math.abs(this.targetY - this.y) <= infinitesimal) {
  //     this.x = this.targetX
  //     this.y = this.targetY
  //   }
  // }

  // // 开始移动
  // moveTo(inputDirection: DIRECTION) {
  //   if (inputDirection === DIRECTION.TOP) {
  //     this.targetY -= 1
  //   } else if (inputDirection === DIRECTION.BOTTOM) {
  //     this.targetY += 1
  //   } else if (inputDirection === DIRECTION.LEFT) {
  //     this.targetX -= 1
  //   } else if (inputDirection === DIRECTION.RIGHT) {
  //     this.targetX += 1
  //   } else if (inputDirection === DIRECTION.TURN_LEFT) {
  //     // 改变人物的方向
  //     if (this.direction === DIRECTION.TOP) {
  //       this.direction = DIRECTION.LEFT
  //     } else if (this.direction === DIRECTION.LEFT) {
  //       this.direction = DIRECTION.BOTTOM
  //     } else if (this.direction === DIRECTION.BOTTOM) {
  //       this.direction = DIRECTION.RIGHT
  //     } else if (this.direction === DIRECTION.RIGHT) {
  //       this.direction = DIRECTION.TOP
  //     }
  //     this.state = PLAYER_STATE_TYPE_ENUM.TURN_LEFT
  //   }
  // }

}
